package com.bfly.mall.product.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.product.dto.ProductSkuListDTO;
import com.bfly.mall.product.entity.ProductSku;
import com.bfly.mall.product.service.IProductSkuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品Sku Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 19:42
 */
@RestController
@RequestMapping(value = "/manage/product")
public class ProductSkuController extends BaseController {

    @Autowired
    private IProductSkuService productSkuService;

    /**
     * 所有产品名称等信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/21 19:43
     */
    @GetMapping(value = "/skus")
    @PermissionCheck(required = false)
    public void getProductSkus(HttpServletResponse response, @RequestParam("productId") int productId) {
        List<ProductSku> productSkus = productSkuService.getProductSku(productId);

        List<ProductSkuListDTO> skuListDTOList = new ArrayList<>();
        if (productSkus != null) {
            productSkus.forEach(sku -> {
                ProductSkuListDTO dto = new ProductSkuListDTO();
                BeanUtils.copyProperties(sku, dto);
                skuListDTOList.add(dto);
            });
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(skuListDTOList));
    }
}